vector data - shapefiles

reading vector data into R

Using package rgdal we can use the gdal drivers to read ESRI shapefiles. As shapefiles have layers, we need to know which layer to load

library(rgdal)
## Loading required package: sp
## rgdal: version: 1.1-3, (SVN revision 594)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.2, released 2015/02/10
##  Path to GDAL shared files: /usr/share/gdal/1.11
##  Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
##  Path to PROJ.4 shared files: (autodetected)
##  Linking to sp version: 1.2-1
library(mapview)
## Loading required package: leaflet

first we set our working directory

setwd('/home/ede/tappelhans/uni/marburg/lehre/2016/ss/PS_global_change')

then we can start working

shp_file <- "data/gadm_deu/gadm_deu.shp"

lyr <- ogrListLayers(dsn = shp_file)
lyr
## [1] "gadm_deu"
## attr(,"driver")
## [1] "ESRI Shapefile"
## attr(,"nlayers")
## [1] 1
gadm_deu <- readOGR(dsn = shp_file,
                    layer = lyr)
## OGR data source with driver: ESRI Shapefile 
## Source: "data/gadm_deu/gadm_deu.shp", layer: "gadm_deu"
## with 403 features
## It has 15 fields
#plot(gadm_deu)
mapview(gadm_deu, color = "blue")

writing vector data to disk

the same drivers also handle writing of shapefiles

writeOGR(obj = gadm_deu,
         dsn = "data/gadm_deu/my_shape.shp",
         layer = "my_shape",
         driver = "ESRI Shapefile",
         overwrite_layer = TRUE)

the driver argument can be used to write many different file formats

ogrDrivers()
##              name write
## 1      AeronavFAA FALSE
## 2          ARCGEN FALSE
## 3          AVCBin FALSE
## 4          AVCE00 FALSE
## 5             BNA  TRUE
## 6         CartoDB FALSE
## 7         CouchDB  TRUE
## 8             CSV  TRUE
## 9             DGN  TRUE
## 10           DODS FALSE
## 11            DXF  TRUE
## 12         EDIGEO FALSE
## 13  ElasticSearch  TRUE
## 14 ESRI Shapefile  TRUE
## 15     Geoconcept  TRUE
## 16        GeoJSON  TRUE
## 17       Geomedia FALSE
## 18         GeoRSS  TRUE
## 19            GFT  TRUE
## 20            GME  TRUE
## 21            GML  TRUE
## 22            GMT  TRUE
## 23           GPKG  TRUE
## 24       GPSBabel  TRUE
## 25  GPSTrackMaker  TRUE
## 26            GPX  TRUE
## 27            HTF FALSE
## 28         Idrisi FALSE
## 29     Interlis 1  TRUE
## 30     Interlis 2  TRUE
## 31            KML  TRUE
## 32         LIBKML  TRUE
## 33   MapInfo File  TRUE
## 34         Memory  TRUE
## 35   MSSQLSpatial  TRUE
## 36          MySQL  TRUE
## 37            NAS FALSE
## 38           ODBC  TRUE
## 39            ODS  TRUE
## 40           OGDI FALSE
## 41        OpenAir FALSE
## 42    OpenFileGDB FALSE
## 43            OSM FALSE
## 44         PCIDSK  TRUE
## 45            PDF  TRUE
## 46            PDS FALSE
## 47         PGDump  TRUE
## 48           PGeo FALSE
## 49     PostgreSQL  TRUE
## 50            REC FALSE
## 51            S57  TRUE
## 52           SDTS FALSE
## 53       SEGUKOOA FALSE
## 54           SEGY FALSE
## 55         SQLite  TRUE
## 56            SUA FALSE
## 57            SVG FALSE
## 58            SXF FALSE
## 59          TIGER  TRUE
## 60        UK .NTF FALSE
## 61            VFK FALSE
## 62            VRT FALSE
## 63           Walk FALSE
## 64           WAsP  TRUE
## 65            WFS FALSE
## 66            XLS FALSE
## 67           XLSX  TRUE
## 68         XPlane FALSE

raster data - geotifs

reading single raster data into R

With package raster it is easy to read valid raster formats such as GeoTiff. We simply need to pass the path to the file to the raster() function

library(raster)

tmean01 <- raster("data/tmean_deu/tmean_deu_m01.tif")
tmean01
## class       : RasterLayer 
## dimensions  : 187, 220, 41140  (nrow, ncol, ncell)
## resolution  : 0.04166667, 0.04166667  (x, y)
## extent      : 5.875, 15.04167, 47.25, 55.04167  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs 
## data source : /home/ede/tappelhans/uni/marburg/lehre/2016/ss/PS_global_change/data/tmean_deu/tmean_deu_m01.tif 
## names       : tmean_deu_m01 
## values      : -92, 25  (min, max)
#plot(tmean01)
mapview(tmean01)

writing single raster data to disk

For writing raster data we use function writeRaster()

writeRaster(tmean01,
            filename = "data/tmean_deu/my_raster_layer.tif",
            overwrite = TRUE)

for raster data we also have multiple file type options

writeFormats()
##       name        long_name                                
##  [1,] "raster"    "R-raster"                               
##  [2,] "SAGA"      "SAGA GIS"                               
##  [3,] "IDRISI"    "IDRISI"                                 
##  [4,] "IDRISIold" "IDRISI (img/doc)"                       
##  [5,] "BIL"       "Band by Line"                           
##  [6,] "BSQ"       "Band Sequential"                        
##  [7,] "BIP"       "Band by Pixel"                          
##  [8,] "ascii"     "Arc ASCII"                              
##  [9,] "CDF"       "NetCDF"                                 
## [10,] "big"       "big.matrix"                             
## [11,] "ADRG"      "ARC Digitized Raster Graphics"          
## [12,] "BMP"       "MS Windows Device Independent Bitmap"   
## [13,] "BT"        "VTP .bt (Binary Terrain) 1.3 Format"    
## [14,] "CTable2"   "CTable2 Datum Grid Shift"               
## [15,] "EHdr"      "ESRI .hdr Labelled"                     
## [16,] "ELAS"      "ELAS"                                   
## [17,] "ENVI"      "ENVI .hdr Labelled"                     
## [18,] "ERS"       "ERMapper .ers Labelled"                 
## [19,] "GS7BG"     "Golden Software 7 Binary Grid (.grd)"   
## [20,] "GSBG"      "Golden Software Binary Grid (.grd)"     
## [21,] "GTiff"     "GeoTIFF"                                
## [22,] "GTX"       "NOAA Vertical Datum .GTX"               
## [23,] "HDF4Image" "HDF4 Dataset"                           
## [24,] "HFA"       "Erdas Imagine Images (.img)"            
## [25,] "IDA"       "Image Data and Analysis"                
## [26,] "ILWIS"     "ILWIS Raster Map"                       
## [27,] "INGR"      "Intergraph Raster"                      
## [28,] "ISIS2"     "USGS Astrogeology ISIS cube (Version 2)"
## [29,] "KRO"       "KOLOR Raw"                              
## [30,] "LAN"       "Erdas .LAN/.GIS"                        
## [31,] "Leveller"  "Leveller heightfield"                   
## [32,] "netCDF"    "Network Common Data Format"             
## [33,] "NITF"      "National Imagery Transmission Format"   
## [34,] "NTv2"      "NTv2 Datum Grid Shift"                  
## [35,] "PAux"      "PCI .aux Labelled"                      
## [36,] "PCIDSK"    "PCIDSK Database File"                   
## [37,] "PNM"       "Portable Pixmap Format (netpbm)"        
## [38,] "RMF"       "Raster Matrix Format"                   
## [39,] "RST"       "Idrisi Raster A.1"                      
## [40,] "SAGA"      "SAGA GIS Binary Grid (.sdat)"           
## [41,] "SGI"       "SGI Image File Format 1.0"              
## [42,] "Terragen"  "Terragen heightfield"

reading multiple raster data into R

If we want to read more than one raster file at a time, we can use R to list.files() in a folder containing a certain string and then pass this to stack()

fls <- list.files(path = "data/tmean_deu",
                  pattern = glob2rx("tmean_deu_*.tif"),
                  full.names = TRUE)

fls
##  [1] "data/tmean_deu/tmean_deu_m01.tif" "data/tmean_deu/tmean_deu_m02.tif"
##  [3] "data/tmean_deu/tmean_deu_m03.tif" "data/tmean_deu/tmean_deu_m04.tif"
##  [5] "data/tmean_deu/tmean_deu_m05.tif" "data/tmean_deu/tmean_deu_m06.tif"
##  [7] "data/tmean_deu/tmean_deu_m07.tif" "data/tmean_deu/tmean_deu_m08.tif"
##  [9] "data/tmean_deu/tmean_deu_m09.tif" "data/tmean_deu/tmean_deu_m10.tif"
## [11] "data/tmean_deu/tmean_deu_m11.tif" "data/tmean_deu/tmean_deu_m12.tif"
tmean_deu <- stack(x = fls)
tmean_deu
## class       : RasterStack 
## dimensions  : 187, 220, 41140, 12  (nrow, ncol, ncell, nlayers)
## resolution  : 0.04166667, 0.04166667  (x, y)
## extent      : 5.875, 15.04167, 47.25, 55.04167  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs 
## names       : tmean_deu_m01, tmean_deu_m02, tmean_deu_m03, tmean_deu_m04, tmean_deu_m05, tmean_deu_m06, tmean_deu_m07, tmean_deu_m08, tmean_deu_m09, tmean_deu_m10, tmean_deu_m11, tmean_deu_m12 
## min values  :           -92,           -90,           -65,           -32,            14,            47,            67,            65,            39,             1,           -47,           -79 
## max values  :            25,            30,            69,           104,           144,           177,           198,           192,           161,           111,            66,            39

writing multiple raster data to disk

this is also done using writeRaster()

writeRaster(x = tmean_deu,
            filename = "data/tmean_deu/my_raster.tif",
            bylayer = TRUE,
            suffix = "names",
            overwrite = TRUE)

NOTE: bylayer and suffix are only needed if we want to write a RasterStack.